package com.zhoux.others;

import java.util.LinkedList;
import static java.lang.String.join; // 静态导包


/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/19 10:01
 *
 * 本类用于实现求二进制方法
 */
public class Binary_Test {

    public static String toBinaryString(int n){ // 求二进制方法
        LinkedList<String> s = new LinkedList<String>();
        while (n!=0){
            Integer m = n%2;
            s.addFirst(m.toString());
            n = n >> 1;
//            System.out.println(n);
        }
        String citiesCommaSeparated = join("", s);
        return citiesCommaSeparated;
    }

    public static String toBinaryString2(int n){ // 求二进制方法
        StringBuilder s = new StringBuilder("");
        while (n!=0){
            int m = n % 2;
            s.append(m);
            n = n >> 1;
//            System.out.println(n);
        }
        StringBuilder str = s.reverse();
        return str.toString();
    }



    public static void main(String[] args) {
        int n =0;
        for (int i = 1; i <= 5; i++) {
            System.out.println(toBinaryString2(i));
//            System.out.println(Integer.toBinaryString(i));
        }
    }
}

